<template>
  <div id="carsIndex">
    <el-row>
      <!-- 表格查询条件 -->
      <el-col :span="24">
        <el-form :inline="true" ref="stationForm" :model="queryForm">
          <el-form-item>
            <el-input v-model="queryForm.code" placeholder="车辆车牌\品牌\车型号"></el-input>
          </el-form-item>
          <el-form-item>
            <gd_param type="select"
                      paramCode="carType"
                      placeholder="车辆类型"
                      v-model="queryForm.carTypeCode" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.userName" placeholder="驾驶员名字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getTableDatas()">查询</el-button>
            <el-button @click="resetQueryForm" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <!-- 数据表格 -->
      <el-col :span="24">
        <el-row>
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-plus" @click="dialog.isEdit=false;dialog.saveOrEditDiv=true">新增车辆信息</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="moreDeletes">
              <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除</el-button>
            </el-popconfirm>
          </el-col>
        </el-row>
        <el-row style="margin-top:10px;">
          <el-col :span="24">
            <el-table :data="tableData" border style="width: 100%" @selection-change="tableDataSelected">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="code" label="编号"></el-table-column>
              <el-table-column prop="carCode" label="车牌"></el-table-column>
              <el-table-column prop="brand" label="品牌"></el-table-column>
              <el-table-column prop="models" label="车辆型号"></el-table-column>
              <el-table-column prop="carTypeName" label="车辆类型"></el-table-column>
              <el-table-column prop="userName" label="驾驶员"></el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="180">
                <template slot-scope="scope">
                  <el-button @click="rowShow(scope.row)" size="small" icon="el-icon-search" circle title="查看"></el-button>
                  <el-button @click="rowEdit(scope.row)" type="primary" size="small" icon="el-icon-edit" circle title="编辑"></el-button>
                  <el-button @click="rowDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
<!--                  <el-dropdown>-->
<!--                    <span class="el-dropdown-link"><i class="el-icon-arrow-down el-icon&#45;&#45;right"></i></span>-->
<!--                    <el-dropdown-menu slot="dropdown">-->
<!--                      <el-dropdown-item><span @click="rowOrders(scope.row)">关联订单</span></el-dropdown-item>-->
<!--                    </el-dropdown-menu>-->
<!--                  </el-dropdown>-->
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="tableSizeChange"
              @current-change="tableCurrentChange"
              :current-page="pageData.page"
              :page-sizes="[10, 20, 40, 100]"
              :page-size="pageData.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageData.total"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <!-- 添加或修改-业务弹窗 -->
    <el-dialog
      :title="dialog.isEdit == true ? '编辑车辆信息' : '新增车辆信息'"
      :visible.sync="dialog.saveOrEditDiv"
      width="80%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
        <el-form ref="saveOrEditDataForm" :rules="saveOrEditDataFormRules" :model="saveOrEditData" label-width="130px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="车牌" prop="carCode">
                <el-input show-word-limit v-model="saveOrEditData.carCode" placeholder="请输入车牌" maxlength="20"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="车辆品牌" prop="brand">
                <el-input show-word-limit v-model="saveOrEditData.brand" placeholder="请输入车辆品牌" maxlength="20"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="车辆型号" prop="models">
                <el-input show-word-limit v-model="saveOrEditData.models" placeholder="请输入车型号" maxlength="20"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="车辆类型" prop="carTypeCode">
                <gd_param type="select"
                          paramCode="carType"
                          placeholder="车辆类型"
                          @selectChange="carTypeChange"
                          v-model="saveOrEditData.carTypeCode" />              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="驾驶员" prop="userID">
                <el-select style="width: 100%" v-model="saveOrEditData.userID" @change="carUserSelect" filterable clearable placeholder="请选择驾驶员">
                  <el-option
                    v-for="item in userListData"
                    :key="item.staffID"
                    :label="item.name"
                    :value="item.staffID">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="车辆容量" prop="capacity">
                <el-input type="number" show-word-limit v-model="saveOrEditData.capacity" placeholder="请输入大概车辆容量（m³）" maxlength="20"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="满油最大里程数" prop="maxMileage">
                <el-input type="number" show-word-limit v-model="saveOrEditData.maxMileage" placeholder="请输入大概满油最大里程数（千米）" maxlength="20"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="comment">
                <el-input
                  show-word-limit
                  maxlength="150"
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 4 }"
                  placeholder="如需备注输入描述"
                  v-model="saveOrEditData.comment"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="车辆正面">
                <gd_file v-if="dialog.isEdit == false" @uploadSuccess="recordFile1UploadSuccess" />
                <gd_file
                  v-else
                  :fileID="saveOrEditData.fileID1"
                  @uploadSuccess="recordFile1UploadSuccess"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="车辆侧身">
                <gd_file v-if="dialog.isEdit == false" @uploadSuccess="recordFile2UploadSuccess" />
                <gd_file
                  v-else
                  :fileID="saveOrEditData.fileID2"
                  @uploadSuccess="recordFile2UploadSuccess"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="车辆后备箱">
                <gd_file v-if="dialog.isEdit == false" @uploadSuccess="recordFile3UploadSuccess" />
                <gd_file
                  v-else
                  :fileID="saveOrEditData.fileID3"
                  @uploadSuccess="recordFile3UploadSuccess"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

      <!-- 按钮组 -->
      <div style="margin-top: 10px">
        <el-button type="primary" @click="submitSaveOrEdit" :loading="formLoading">{{ dialog.isEdit == false ? '保存' : '确认修改' }}</el-button>
        <el-button
          @click="
							dialog.saveOrEditDiv = false;
							resetAddOrEditOrShowForm();
						"
        >
          取消
        </el-button>
      </div>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog
      title="车辆详情"
      :visible.sync="dialog.showDiv"
      width="90%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >

      <el-row style="margin-top: 10px">
        <el-button
          @click="
							dialog.showDiv = false;
							resetAddOrEditOrShowForm();
						"
        >
          关闭
        </el-button>
      </el-row>
    </el-dialog>



  </div>
</template>

<script>
import gd_param from "@/components/mycomponents/gd_param.vue";
import * as axiosAPI from '@/api/wms/cars.js';
import gd_file from "@/components/mycomponents/gd_file.vue";
import * as staffAPI from '@/api/staff.js';
export default {
  name: 'carsIndex',
  components:{gd_file, gd_param},
  data() {
    return {
      //查询表单数据
      queryForm: {
        code: null,
        carTypeCode: null,
        userName: null
      },
      //表格数据
      tableData: [],
      // 用户列表
      userListData:[],
      //主表-分页数据
      pageData: {
        page: 1,
        size: 10,
        total: 0
      },
      //当前表格复选数据
      nowTableSelected: [],
      //弹窗状态
      dialog: {
        isEdit: false,
        saveOrEditDiv: false,
        showDiv: false
      },
      //添加或编辑表单数据
      saveOrEditData: {
        carID:null,
        comment:null,
        code:null,
        carCode:null,
        brand:null,
        models:null,
        carTypeCode:null,
        carTypeName:null,
        userID:null,
        userName:null,
        capacity:null,
        maxMileage:null,
        fileID1:null,
        fileUrl1:null,
        fileID2:null,
        fileUrl2:null,
        fileID3:null,
        fileUrl3:null
      },
      //添加编辑表单-检验
      saveOrEditDataFormRules:{
        carCode: { required: true, message: '请输入车牌', trigger: 'blur' },
        brand: { required: true, message: '请输入品牌', trigger: 'blur' },
        models: { required: true, message: '请输入车辆型号', trigger: 'blur' },
        carTypeCode: { required: true, message: '请选择车辆类型', trigger: 'change' },
        userID: { required: true, message: '请选择驾驶员', trigger: 'change' },
        capacity: { required: true, message: '请输入最大容量', trigger: 'blur' },
        maxMileage: { required: true, message: '请输入最大里程数', trigger: 'blur' }
      },
      //展示表单数据
      showData: {
        carID:null,
        comment:null,
        code:null,
        carCode:null,
        brand:null,
        models:null,
        carTypeCode:null,
        carTypeName:null,
        userID:null,
        userName:null,
        capacity:null,
        maxMileage:null,
        fileID1:null,
        fileUrl1:null,
        fileID2:null,
        fileUrl2:null,
        fileID3:null,
        fileUrl3:null
      },
      //表单加载值
      formLoading: false
    };
  },
  mounted() {
    this.initUserListData();
    this.getTableDatas();
  },
  methods: {
    // 查询用户列表
    initUserListData(){
      var _this = this;
      staffAPI.list({"name":null,"tellNumber":null})
        .then(function(res){
          if(res.code == "0000"){
            _this.userListData = res.data;
          }
        });
    },
    //查询表格数据
    getTableDatas() {
      let _this = this;
      axiosAPI.pages(_this.queryForm, _this.pageData)
        .then(function(response) {
          console.log(response);
          if (response.code == '0000') {
            _this.tableData = response.data.rows;
            _this.pageData.total = response.data.total;
          }
        });
    },
    //重置查询条件表单
    resetQueryForm() {
      this.queryForm = {
        code: null,
        carTypeCode: null,
        userName: null
      };
      this.getTableDatas();
    },
    //重置添加、编辑、展示表单
    resetAddOrEditOrShowForm() {
      this.showData = {
        carID:null,
        comment:null,
        code:null,
        carCode:null,
        brand:null,
        models:null,
        carTypeCode:null,
        carTypeName:null,
        userID:null,
        userName:null,
        capacity:null,
        maxMileage:null,
        fileID1:null,
        fileUrl1:null,
        fileID2:null,
        fileUrl2:null,
        fileID3:null,
        fileUrl3:null
      };
      this.saveOrEditData = {
        carID:null,
        comment:null,
        code:null,
        carCode:null,
        brand:null,
        models:null,
        carTypeCode:null,
        carTypeName:null,
        userID:null,
        userName:null,
        capacity:null,
        maxMileage:null,
        fileID1:null,
        fileUrl1:null,
        fileID2:null,
        fileUrl2:null,
        fileID3:null,
        fileUrl3:null
      };
    },
    //提交-添加、编辑表单
    submitSaveOrEdit() {
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          let _this = this;
          if(_this.dialog.isEdit == true){// 编辑

            axiosAPI.update(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });

          }else{ // 添加
            axiosAPI.add(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });
          }
        }
      });
    },
    //提交-删除
    submitDelete(ids){
      let _this = this;
      axiosAPI.deletes(ids)
        .then(res => {
          if (res.code == '0000') {
            _this.getTableDatas();
          }
        });
    },
    //分页条目数切换
    tableSizeChange(size) {
      this.pageData.size = size;
      this.getTableDatas();
    },
    //页码切换
    tableCurrentChange(page) {
      this.pageData.page = page;
      this.getTableDatas();
    },
    //行-查看
    rowShow(data) {
      this.showData.record = JSON.parse(JSON.stringify(data));
      this.dialog.showDiv = true;
    },
    //行-编辑
    rowEdit(data) {
      let _this = this;
      axiosAPI.getByID(data.carID)
        .then(function(res){
          if(res.code == "0000"){
            _this.dialog.saveOrEditDiv = true;
            _this.saveOrEditData = res.data;
            _this.dialog.isEdit = true;
          }
        })

    },
    //行-删除
    rowDelete(data) {
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(data.carID);
      });
    },
    //点击批量删除
    moreDeletes() {
      if(this.nowTableSelected.length<=0){
        this.$notify.warning({
          title: '删除提示',
          message: '请至少选择一条数据再删除'
        });
        return;
      }
      let ids = '';

      for (var i in this.nowTableSelected) {
        ids += this.nowTableSelected[i].carID + ',';
      }

      ids = ids.substring(0, ids.length - 1);

      //调用删除
      this.submitDelete(ids);
    },
    //表格批量选择
    tableDataSelected(data){
      this.nowTableSelected = data;
    },
    // 出库人选择
    carUserSelect(data){
      if(data!=""){
        var resData = null;
        for(var i in this.userListData){
          if(this.userListData[i].staffID == data){
            resData = this.userListData[i];
            break;
          }
        }
        this.saveOrEditData.userID = resData.staffID;
        this.saveOrEditData.userName = resData.name;
      }else{
        this.saveOrEditData.userID = null;
        this.saveOrEditData.userName = null;
      }
    },
    // 车辆类型选择切换
    carTypeChange(data){
      if(data == undefined){
        this.saveOrEditData.carTypeCode = null;
        this.saveOrEditData.carTypeName = null;
      }else{
        this.saveOrEditData.carTypeCode = data.dataValue;
        this.saveOrEditData.carTypeName = data.dataName;
      }
    },
    // 图片上传切换1
    recordFile1UploadSuccess(data){
      if(data.length==0){
        this.saveOrEditData.fileID1 = null;
        this.saveOrEditData.fileUrl1 = null;
      }else{
        this.saveOrEditData.fileID1 = data[0].id;
        this.saveOrEditData.fileUrl1 = data[0].url;
      }
    },
    // 图片上传切换2
    recordFile2UploadSuccess(data){
      if(data.length==0){
        this.saveOrEditData.fileID2 = null;
        this.saveOrEditData.fileUrl2 = null;
      }else{
        this.saveOrEditData.fileID2 = data[0].id;
        this.saveOrEditData.fileUrl2 = data[0].url;
      }
    },
    // 图片上传切换3
    recordFile3UploadSuccess(data){
      if(data.length==0){
        this.saveOrEditData.fileID3 = null;
        this.saveOrEditData.fileUrl3 = null;
      }else{
        this.saveOrEditData.fileID3 = data[0].id;
        this.saveOrEditData.fileUrl3 = data[0].url;
      }
    }

  }
};
</script>

<style scoped></style>
